/**
 * 
 */
package pl.prv.polanie.client.widgets.schedule.custom;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Janusz Prokulewicz
 * Created: Jun 18, 2008
 */
public class MyButton extends Composite {
	
	private static final String STYLE_MYBUTTON = "swo-MyButton";
//	private static final String STYLE_MYBUTTON_ACTIVE = "swo-MyButton-active";
	private static final String STYLE_MYBUTTON_INACTIVE = "swo-MyButton-inactive";
	
	
	

	private String htmlOn = "on";
	private String htmlOff = "off";
	private String htmlInactive = "inactive";
	

	private ClickListenerCollection clickListeners;
	private HTML mainHTML = new HTML();
	
	
	
	private boolean checked = false;
	private boolean active = true;
	
	
	
	public MyButton(String html) {
		initWidget(mainHTML);
		mainHTML.setHTML(html);
		htmlOn = html;
		htmlOff = html;
		setupMainHTML();
	}
	
	
	private void setupMainHTML() {
		mainHTML.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if (active) {
					checked =! checked;
					
					mainHTML.setHTML(htmlOn);
					if (checked) {
						mainHTML.removeStyleName(STYLE_MYBUTTON_INACTIVE);
					} else {
//						mainHTML.setHTML(htmlOff);
						mainHTML.addStyleName(STYLE_MYBUTTON_INACTIVE);
					}
					
					
			        if (clickListeners != null) {
			            clickListeners.fireClick(MyButton.this);
			        }
				} else {
					Window.alert("asd");
				}
				
			}
		});
		
		mainHTML.addStyleName(STYLE_MYBUTTON);
	}
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public void addClickListener(ClickListener listener) {
		if (clickListeners == null) {
			clickListeners = new ClickListenerCollection();
		}
		clickListeners.add(listener);
	}
	
	public void removeClickListener(ClickListener listener) {
		if (clickListeners != null) {
			clickListeners.remove(listener);
		}
	}


	
	
	
	public boolean isChecked() {
		return checked;
	}


	public void setChecked(boolean checked) {
		this.checked = checked;

		if (checked) {
			mainHTML.removeStyleName(STYLE_MYBUTTON_INACTIVE);
			mainHTML.setHTML(htmlOn);
		} else {
			mainHTML.setHTML(htmlOff);
			mainHTML.addStyleName(STYLE_MYBUTTON_INACTIVE);
		}
	}


	public boolean isActive() {
		return active;
	}


	public void setActive(boolean active) {
		this.active = active;
	}
	
}
